/*
 *
 * This program is free software; you can redistribute it and/or modify 
 * it under the terms of the GNU General Public License version 2 as 
 * published by the Free Software Foundation.
 *
 * thread_pool.h is for what ...
 *
 * Version: ***: thread_pool.h  Sat Jul  2 09:57:22 2011 poponion Exp $
 *
 * Authors:
 *   Author poponion <huangcm85@gmail.com>
 *     -some work detail if you want 
 *
 */
#ifndef THREAD_POOL_H_
#define THREAD_POOL_H_

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
#include "default_task.h"
#include "list.h"

class ThreadPool
{
public:
  
  /**
   * Add task to thread pool
   *
   * @param task  task to add to thread pool
   * 
   * @return int  if success, return 0, else return -1
   */
  int add_task(DefaultTask& task);
  
  /**
   * Initialize thread pool, create maxThread thread
   * 
   * @param thread  thread num
   *
   * @return int if success, reutrn 0, else return -1
   */
  int init(int thread);

  /**
   * thread call back 
   */
  static void* thread_run(void *arg);
  
  void * run();
  
  /**
   * Destroy thread resources
   */
  void destroy();
public:
  List<DefaultTask> tasks_;
private:
  pthread_mutex_t mutex_;
  pthread_cond_t cond_;
  pthread_t * threads_;
  int thread_num_;
  bool init_;
  
};
#endif
